package com.zzsoft.girl.controller;

import com.zzsoft.girl.aspect.HttpAspect;
import com.zzsoft.girl.domain.Girl;
import com.zzsoft.girl.domain.Result;
import com.zzsoft.girl.repository.GirlRepository;
import com.zzsoft.girl.service.GirlService;
import com.zzsoft.girl.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by zhanghb on 2018/7/28.
 */
@RestController
public class GirlController {

    private final static Logger logger = LoggerFactory.getLogger(GirlController.class);

    @Autowired
    private GirlRepository girlRepository;

    @Autowired
    private GirlService girlService;

    /**
     * 查询所有女生列表
     * @return
     */
    @GetMapping(value = "/girls")
    public List<Girl> girllist(){
        logger.info("girllist");
        return girlRepository.findAll();
    }

    /**
     * 添加女生
     * 使用PostMan测试时，body需要选择数据类型为：x-www-form-urlencoded
     * @param girl
     * @return
     */
    @PostMapping(value = "/girls")
    public  Result<Girl> girlAdd(@Valid Girl girl, BindingResult bindingResult){
         if(bindingResult.hasErrors()){
             return ResultUtil.error(1,bindingResult.getFieldError().getDefaultMessage());
         }
         girl.setCupSize(girl.getCupSize());
         girl.setAge(girl.getAge());
         //girl.setMoney(girl.getMoney());
        return ResultUtil.success(girlRepository.save(girl));
    }

    /**
     * 根据id查询女生
     * @param id
     * @return
     */
    @GetMapping(value = "/girls/{id}")
    public Girl girlFindOne(@PathVariable("id") Integer id){
         Girl girl =  girlRepository.findOne(id);
         return  girl;
    }

    /**
     * 更新
     * 使用PostMan测试时，body需要选择数据类型为：x-www-form-urlencoded
     * @param id
     * @param cupSize
     * @param age
     * @return
     */
    @PutMapping(value = "/girls/{id}")
    public Girl girlUpdate(@PathVariable("id") Integer id,
                           @RequestParam("cupSize") String cupSize,
                           @RequestParam("age") Integer age){
        Girl girl = new Girl();
        girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);
        return  girlRepository.save(girl);

    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping(value = "/girls/{id}")
    public void girlDelete(@PathVariable("id") Integer id){
        girlRepository.delete(id);
    }

    /**
     * 根据年龄查询女生列表
     * @param age
     * @return
     */
    @GetMapping(value = "/girls/age/{age}")
    public List<Girl> girlListByAge(@PathVariable("age") Integer age){
        return girlRepository.findByAge(age);
    }
    @PostMapping(value = "/girls/two")
    public void girlTwo(){
        girlService.insertTwo();
    }
    @GetMapping("/girls/getAge/{id}")
    public void getAge(@PathVariable("id") Integer id)throws Exception{
        girlService.getAge(id);
    }
}
